package com.peng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.peng.constant.Constants;
import com.peng.manager.RedisManager;
import com.peng.model.TPermission;
import com.peng.model.TRole;
import com.peng.model.TUser;
import com.peng.query.UserQuery;
import com.peng.service.TUserService;
import com.peng.mapper.TUserMapper;
import com.peng.util.CacheUtils;
import com.peng.util.JWTUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author Love-ovo
* @description 针对表【t_user(用户表)】的数据库操作Service实现
* @createDate 2025-03-14 21:28:15
*/



@Service
public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser>
    implements TUserService{

    //密码加密器
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private TUserMapper tUserMapper;

    @Autowired
    private RedisManager redisManager;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveUser(UserQuery userQuery) {
        //将TUser当中符合userQuery属性copy过来
        TUser tUser = new TUser();
        BeanUtils.copyProperties(userQuery, tUser);

        //将密码加密
        tUser.setLogin_pwd(passwordEncoder.encode(userQuery.getLogin_pwd()));
        //创建时间
        tUser.setCreate_time(new Date());

        //利用 token 获取创建人信息
        //登录人的id
        Integer loginUserId = JWTUtils.parseUserFromJWT(userQuery.getToken()).getId();
        tUser.setCreate_by(loginUserId); //创建人

        return tUserMapper.insert(tUser);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateUser(UserQuery userQuery) {
        //将TUser当中符合userQuery属性copy过来
        TUser tUser = new TUser();
        BeanUtils.copyProperties(userQuery, tUser);

        //有密码则将密码加密，否则不管
        //前端传回的是null
        if(tUser.getLogin_pwd() != null){
            tUser.setLogin_pwd(passwordEncoder.encode(userQuery.getLogin_pwd()));
        }

        //编辑时间
        tUser.setEdit_time(new Date());

        //利用 token 获取编辑人信息
        //登录人的id
        Integer loginUserId = JWTUtils.parseUserFromJWT(userQuery.getToken()).getId();
        tUser.setEdit_by(loginUserId); //创建人

        LambdaQueryWrapper<TUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TUser::getId,tUser.getId());

        return tUserMapper.update(tUser,queryWrapper);
    }

    @Override
    public List<TUser> getOwnerList() {
        //1、从redis查询
        //2、redis查不到，就从数据库查询，并且把数据放入redis（5分钟过期）
        return CacheUtils.getCacheData(() -> {
                    //生产，从缓存redis查询数据
                    return (List<TUser>)redisManager.getValue(Constants.REDIS_OWNER_KEY);
                },
                () -> {
                    //生产，从mysql查询数据
                    return tUserMapper.selectByOwner();
                },
                (t) -> {
                    //消费，把数据放入缓存redis
                    redisManager.setValue(Constants.REDIS_OWNER_KEY, t);
                }
        );
    }
}




